package com.o2o.util;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.safety.Whitelist;
import org.jsoup.select.Elements;

public class JsoupUtil {
	private final static Whitelist user_content_filter = Whitelist.relaxed();
	/**
	 * 允许的标签: a, b, blockquote, br, caption, cite, code, col, colgroup, dd, dl, dt, 
	 * em, h1, h2, h3, h4, h5, h6, i, img, li, ol, p, pre, q, small, strike, strong, sub, sup,
	 * table, tbody, td, tfoot, th, thead, tr, u, ul。结果不包含标签rel=nofollow ，如果需要可以手动添加。
	 */
	static {
	    user_content_filter.addTags("embed","object","param","span","div");
	    user_content_filter.addAttributes(":all", "style", "class", "id", "name");
	    user_content_filter.addAttributes("object", "width", "height","classid","codebase");   
	    user_content_filter.addAttributes("param", "name", "value");
	    user_content_filter.addAttributes("embed", "src","quality","width","height","allowFullScreen","allowScriptAccess","flashvars","name","type","pluginspage");
	}
	
	/**
	 * 对用户输入内容进行过滤
	 * @param html
	 * @return
	 */
	public static String filterUserInputContent(String html) {
	    if(StringUtils.isBlank(html)) return "";
	    return Jsoup.clean(html, user_content_filter);
	    //return filterScriptAndStyle(html);
	}


	/**
	 * 解析html，过滤样式标签等,进行存储
	 * @param html
	 * @return
	 */
	public static String parseHtml(String html){
		if(html == null || "".equals(html)){
			return null;
		}
		try {
			String filterHtml = JsoupUtil.filterUserInputContent(html);
			System.out.println(filterHtml);
			return filterHtml;
//			Document doc = Jsoup.parse(filterHtml);
//			Elements imgList = doc.select("img");//找到所有img标签
//			Elements videoList = doc.select("embed[class=edui-faked-video]");//查找所有视频标签
//			Elements musicList = doc.select("embed[class=edui-faked-music]");//查找所有音乐标签
//			for (Element img : imgList) {
//				String imgSrc = img.attr("src");//图片地址
//				img.parent().append("<img src=\""+imgSrc+"\"/>");
//				img.remove();
//			}
//			for(Element video : videoList){
//				String embedSrc = video.attr("src");
//				video.parent().append("<video src=\""+embedSrc+"\"/>");
//				video.remove();
//			}
//			for(Element music : musicList){
//				String embedSrc = music.attr("src");
//				music.parent().append("<music src=\""+embedSrc+"\"/>");
//				music.remove();
//			}
////		System.out.println("result html:"+resultHtml);
//			return filterHtml;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 反向解析html，生成可视的页面标签
	 * @param html
	 * @return
	 */
	public static String unParseHtml(String html){
		if(html == null || "".equals(html)){
			return null;
		}
		try {
			html = "<div id='content_text'>"+html+"</div>";
			Document doc = Jsoup.parse(html);
			Elements imgList = doc.select("img");//找到所有img标签
			Elements videoList = doc.select("embed[class=edui-faked-video]");//查找所有视频标签
			Elements musicList = doc.select("embed[class=edui-faked-music]");//查找所有音乐标签
			for (Element img : imgList) {
				/*img.wrap("<div></div");*/
			}
			for(Element video : videoList){
				video.attr("flashvars", "playMovie=true&auto=1&adss=0&isAutoPlay=true");
				video.attr("allowfullscreen","true");
				video.wrap("<div class='wrap_emped_div'></div>");
				video.before("<div class='focus_div'><a href='javascript:;' onclick='openEmbed(this);'><img src='/o2o-online/image/f052332fc146c97ba8d56f976ed9a2c8.jpg' style='cursor: url(http://tb2.bdstatic.com/tb/static-pb/img/cur_zin.cur), pointer;' width='420px' height='280px'/></a></div>");
				video.wrap("<div style='display:none' class='emb_div'><a href='javascript:;' onclick='hideEmbed(this)'>收起</a>");
			}
//			for(Element music : musicList){
//				String embedSrc = music.attr("src");
//				music.parent().append("<music src=\""+embedSrc+"\"/>");
//				music.remove();
//			}
			Elements contentDiv = doc.select("div#content_text");
			return contentDiv.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 解析html，为消息通知类型的文本
	 * @param html
	 * @return
	 */
	public static String parseHtmlToNotice(String html){
		if(html == null || "".equals(html)){
			return null;
		}
		try {
			String filterHtml = JsoupUtil.filterUserInputContent(html);
			Document doc = Jsoup.parse(filterHtml);
			Elements imgList = doc.select("img");//找到所有img标签
			Elements videoList = doc.select("embed[class=edui-faked-video]");//查找所有视频标签
			Elements musicList = doc.select("embed[class=edui-faked-music]");//查找所有音乐标签
			for (Element img : imgList) {
				img.parent().append("[图片]");
				img.remove();
			}
			for(Element video : videoList){
				video.parent().append("[视频]");
				video.remove();
			}
			for(Element music : musicList){
				music.parent().append("[音乐]");
				music.remove();
			}
			Elements pEles = doc.select("p");
			StringBuffer result = new StringBuffer();
			for (Element p : pEles) {
				result.append(p.html());
			}
			String resultHtml = result.toString();
//		System.out.println("result html:"+resultHtml);
			return resultHtml;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
